//
//  FlowButtonView.m
//  nacf
//
//  Created by liwei on 15/9/30.
//  Copyright © 2015年 liwei. All rights reserved.
//

#import "FlowButtonView.h"
#import "SelectConditionModel.h"

#define kCellBorder 8
#define kCellXSpace 22
#define kCellYSpace 12
#define kContentFont [UIFont systemFontOfSize:16]

@implementation FlowButtonView

- (void)updateLayout {
    for(UIView *view in [self subviews]) {
        [view removeFromSuperview];
    }
    if (self.titleList.count <= 0) {
        return;
    } else {
        float x = 0;
        float y = kCellBorder + kCellYSpace;
        int i = 0;
        for (SelectConditionModel *model in self.titleList) {
            UIButton *btn = [[UIButton alloc] init];
            btn.tag = i;
            [btn setTitle:model.Name forState:UIControlStateNormal];
            btn.titleLabel.font = kContentFont;
            //不同状态不同颜色
            if (model.isSelect) {
                [btn setTitleColor:GET_HEX_COLOR(NAVBAR_BG_COLOR) forState:UIControlStateNormal];
            } else {
                [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
            }
            CGSize titleSize = [model.Name sizeWithAttributes: @{NSFontAttributeName:kContentFont}];
            if (x + titleSize.width + kCellBorder + kCellXSpace > self.width) {
                x = titleSize.width + kCellBorder + kCellXSpace;
                y += titleSize.height + kCellBorder + kCellYSpace;
            } else {
                x += titleSize.width + kCellBorder + kCellXSpace;
            }
            btn.frame = CGRectMake(x - titleSize.width - kCellXSpace, y - kCellYSpace, titleSize.width + kCellXSpace, titleSize.height + kCellYSpace);
            btn.backgroundColor = [UIColor whiteColor];
            btn.layer.cornerRadius = 5;
            btn.layer.borderWidth = 1;
            btn.layer.borderColor = [UIColor lightGrayColor].CGColor;
            [btn addTarget:self action:@selector(click_tag:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:btn];
            if (i == self.titleList.count - 1) {
                y += titleSize.height + kCellBorder;
            }
            i++;
        }
        self.frame = CGRectMake(0, 0, self.width, y);
    }
}

- (void)setWidth:(float)width {
    _width = width;
    [self updateLayout];
}

- (void)setTitleList:(NSArray *)titleList {
    _titleList = titleList;
    [self updateLayout];
}

- (void) click_tag: (UIButton *) btn {
    SelectConditionModel *model = _titleList[btn.tag];
    if (model.isSelect) {
        model.isSelect = NO;
        [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    } else {
        model.isSelect = YES;
        [btn setTitleColor:GET_HEX_COLOR(NAVBAR_BG_COLOR) forState:UIControlStateNormal];
    }
    if (self.delegate) {
        [self.delegate selectAtIndex:(int)btn.tag Title:self.titleList[btn.tag] inFlowTag:self];
    }
}

@end
